package worldutils.huntgame.Listener;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import worldutils.huntgame.HuntGame;

import java.util.Arrays;

public class AirDropSet implements Listener {
    @EventHandler
    public static void onAirDropSetMenuClose(InventoryCloseEvent event){
        Inventory inventory = event.getInventory();
        Player player = (Player) event.getPlayer();

        if (event.getView().getTitle().equalsIgnoreCase("设置AirDrop")){
            StringBuilder jsonBuilder = new StringBuilder("{ \"items\": [");

            boolean first = true;
            for (int i = 0; i < inventory.getContents().length; i++) {
                ItemStack item = inventory.getContents()[i];
                if (item != null && item.getType() != Material.AIR) {
                    if (!first) {
                        jsonBuilder.append(", ");
                    } else {
                        first = false;
                    }

                    String itemName = item.hasItemMeta() && item.getItemMeta().hasDisplayName()
                            ? item.getItemMeta().getDisplayName()
                            : item.getType().toString();

                    int amount = item.getAmount();
                    String enchantments = item.getEnchantments().isEmpty() ? "无" : item.getEnchantments().toString();

                    jsonBuilder.append("{")
                            .append("\"position\": ").append(i).append(", ")
                            .append("\"name\": \"").append(itemName).append("\", ")
                            .append("\"amount\": ").append(amount).append(", ")
                            .append("\"enchantments\": \"").append(enchantments).append("\"")
                            .append("}");
                }
            }

            jsonBuilder.append("]}");

            player.sendMessage(jsonBuilder.toString());
            System.out.println(jsonBuilder.toString());
            HuntGame.AirDropChestNR = jsonBuilder.toString();
        }
    }

    public static Inventory jsonToInventory(String json) {
        Inventory inventory = Bukkit.createInventory(null, 27, "设置AirDrop");

        JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
        JsonArray items = jsonObject.getAsJsonArray("items");

        for (JsonElement element : items) {
            JsonObject itemObject = element.getAsJsonObject();
            int position = itemObject.get("position").getAsInt();
            String itemName = itemObject.get("name").getAsString();
            int amount = itemObject.get("amount").getAsInt();
            ItemStack itemStack = new ItemStack(Material.matchMaterial(itemName), amount);

            // if (!itemObject.get("enchantments").getAsString().equals("无")) {
            // }

            inventory.setItem(position, itemStack);
        }

        return inventory;
    }
}
